// 2013-7-29下午06:21:38  net.betou.trade.main.manager.impl.NewsMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.entity.ConfigLogin;
import net.betou.core.service.PostDataListener;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.News;
import net.betou.trade.main.dao.NewsDao;
import net.betou.trade.main.manager.NewsMng;
import net.betou.trade.role.setting.CommonFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class NewsMngImpl implements NewsMng{

	public News deleteById( Integer id ) {
		News bean = dao.deleteById( id );
		afterDelete( bean );
		
		return bean;
	}

	public News [ ] deleteByIds( Integer [ ] ids ) {
		News [ ] beans = new News[ ids.length ];
		
		for( int i = 0; i < ids.length; i++ ){
			beans[ i ] = deleteById( ids[ i ] );
		}
		return beans;
	}

	@Transactional( readOnly = true )
	public News findById( Integer id ) {
		return dao.findById( id );
	}
	
	@Transactional( readOnly = true )
	public News findMyNews( Integer id, Integer selfId, Boolean system ) {
		News notice;
		if( system ){
			notice = dao.findById( id );
		}else{
			notice = dao.findMyNews( id, selfId );
		}
		
		return notice;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection,  int pageNo, int pageSize ) {
		Boolean system = admin.getSystem( );
		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}else{
			// 要查询自己的和系统的公告
			
			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_NEWS );
			
			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
			}else{
				
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}
		
		return pagination;
	}

	public News save( News bean, AgentAndAdmins admin ) {
		bean.init( );
		
		bean.setAdmin( admin );
		
		dao.save( bean );
		
		afterSave( bean );
		return bean;
	}

	public News update( News bean ) {
		Updater< News > updater = new Updater< News >( bean, Updater.UpdateMode.MIN ); 
		updater.include( "title" ); // 使用最小模式
		updater.include( "content" );
		return dao.updateByUpdater( updater );
	}
	
	private void afterSave( News notice ) {
		if( listenerList != null ){
			for ( PostDataListener< News > listener : listenerList ){
				listener.afterSave( notice );
			}
		}
	}
	
	private void afterDelete( News notice ){
		if( listenerList != null ){
			for ( PostDataListener< News > listener : listenerList ){
				listener.afterDelete( notice );
			}
		}
	}
	
	private List< PostDataListener< News > > listenerList;

	public void setListenerList( List< PostDataListener< News > > listenerList ) {
		this.listenerList = listenerList;
	}
	
	@Autowired
	private NewsDao dao;
	

}
